home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / mac.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  150 lines

  1. /*
  2.  * $Id: mac.h,v 0.91 1994/02/20 00:06:08 zhao Pre-Release $
  3.  *
  4.  *. This file is part of BIT shareware package. After the two weeks of 
  5.  *  free evaluation period, you are encouraged (required) to register 
  6.  *  your copy for a small registration fee, which is $35 for personal use
  7.  *  and $50 for commercial, government and institutional use.
  8.  *  
  9.  *  Copyright(c) 1993, 1994 by T.C. Zhao.
  10.  *  All rights reserved.
  11.  *
  12.  *  Permission to use, copy, and distribute this software in its entirety
  13.  *  for non-commercial purposes is hereby granted, provided that the 
  14.  *  above shareware and copyright notices and this permission notice
  15.  *  appear in all copies and their documentation.
  16.  *
  17.  *  This software may be modified for your own use, but modified versions
  18.  *  may not be distributed without prior consent of the author.
  19.  *
  20.  *  This software is provided "as is" without expressed or implied
  21.  *  warranty of any kind.
  22.  *
  23.  *.
  24.  * Some useful macros.  Usual restrictions apply.
  25.  * This file is #include'd by uproto.h as well as bit.h and
  26.  * maybe by others
  27.  */
  28.  
  29. #ifndef BIT_MAC_H
  30. #define BIT_MAC_H
  31.  
  32. #ifdef lint
  33.   extern const int ZERO;
  34. #else
  35. #  ifdef ZERO
  36. #     undef ZERO
  37. #  endif
  38. #  define ZERO    0
  39. #endif
  40.  
  41. /* General utility macros */
  42.  
  43. #ifndef Abs
  44. #define Abs(a)      (((a) > 0) ? (a) : -(a))
  45. #endif   /* Abs */
  46.  
  47. #ifndef Sgn
  48. #define Sgn(a)      ((a < 0) ? -1: 1)
  49. #endif
  50.  
  51. #ifndef  Min
  52. #define  Min(a,b)   (((a) < (b)) ? (a):(b))
  53. #endif
  54.  
  55. #ifndef Max
  56. #define  Max(a,b)   (((a) > (b)) ? (a):(b))
  57. #endif
  58.  
  59. /* might speed things up a bit */
  60. #ifndef Streq
  61. #define Streq(a,b)  (*(a) == *(b) && strcmp((a),(b)) == 0)
  62. #endif
  63.  
  64. /* terminates strncpy properly, but failed the original return */
  65. #ifndef Strncpy
  66. #define Strncpy(s1,s2,n)    sprintf(s1, "%.*s", (int)n,  s2)
  67. #endif
  68.  
  69. #ifndef Badfread
  70. #define Badfread(buf, es, n, fp)   (fread(buf, es, n, fp) != (n))
  71. #define Badfwrite(buf, es, n, fp)  (fwrite(buf, es, n, fp) != (n))
  72. #endif
  73.  
  74. #ifndef Badfgets
  75. #define Badfgets(buf, n, fp)       (fgets(buf, n, fp) == 0)
  76. #define Badfputs(buf, n, fp)       (fputs(buf, n, fp) == 0)
  77. #endif
  78.  
  79. /* Just make a safe free  */
  80. #ifndef Free
  81. #define Free(p)     do { if(p) free(p); (p) = 0;} while(ZERO)
  82. #endif
  83.  
  84. #ifndef Range
  85. #define Range(x, xmin, xmax)             \
  86.         do {                             \
  87.           if( x < (xmin)) x = (xmin);    \
  88.           else if(x > (xmax)) x = (xmax);\
  89.         } while (ZERO)
  90. #endif
  91.  
  92. #ifndef Swap
  93. #define Swap(type, a, b )                   \
  94.             do {                            \
  95.               register type tmp__ = (a);    \
  96.               (a) = (b); (b) = tmp__;       \
  97.             } while (ZERO)
  98. #endif
  99.  
  100. #ifndef Lintp  /* linear interpolation with delta = 1 */
  101. #define Lintp(a,b,dx)  (((dx) < 0.004) ? (a) : ((a) + ((b) - (a))*(dx)))
  102. #endif
  103.  
  104. /* check if the point is within a rectangular region */
  105. #ifndef Inside
  106. #define Inside(x,y,x1,y1,x2,y2)    \
  107.           ((x) >= (x1) && (x) <= (x2)  && (y) >= (y1) && (y) <= (y2))
  108. #endif
  109.  
  110. /* macros specific to BIT program */
  111.  
  112. #define REPORT(j,t) if(((j)+1)%t==0) update_progress_report((j)+1)
  113.  
  114. #define SET_NICE(a)          \
  115.          do {                \
  116.             if((a) > 1000) (a) = ((a)/1000) * 1000;  \
  117.             else if((a) > 100) (a) = ((a)/100) * 100;\
  118.             else if((a) > 10 ) (a) = ((a)/10)  * 10; \
  119.             else if((a) > 5  ) (a) = 5;              \
  120.             else a = 1;                              \
  121.          } while (ZERO)
  122. /*
  123.  * A bi-linear interpolation: get the value at (r1+dx,c1+dy).
  124.  * r,c indicates the row and column
  125.  * po     : the output
  126.  * (r1,c1): the approximate grid point           O(r2,c1)     O(r2,c2)
  127.  * (r2,c2): the approximate grid point
  128.  * (dr,dc): delta-x and -y from the grid point           o(r1+dr,c1+dc)
  129.  * drc    : dr*dc;                               O(r1,c1)     O(r1,c2)
  130.  * pc     : source
  131.  *
  132.  */
  133. #define GETFRAC(po,r1,c1,r2,c2,dr,dc,drc,pc,min,max)         \
  134.        do {                                                  \
  135.           register float pdc_, pdr_, pf_, f_;                \
  136.           register int itmp_;                                \
  137.           f_   = (float) pc[r1][c1];                         \
  138.           pdr_ = (float) pc[r2][c1] - f_;                    \
  139.           pdc_ = (float) pc[r1][c2] - f_;                    \
  140.           pf_  = (float) pc[r2][c2] - f_;                    \
  141.           f_ += pdc_ * (dc) + pdr_ * (dr) +                  \
  142.                 (pf_ - pdc_ - pdr_) * (drc);                 \
  143.           itmp_ = (f_ + 0.5);                                \
  144.           if(itmp_ > max) itmp_ = max;                       \
  145.           if(itmp_ < min) itmp_ = min;                       \
  146.           (po) = itmp_;                                      \
  147.         } while (ZERO)
  148.  
  149. #endif
  150.